package cn.edu.zafu.bigdata.weier;

import java.util.*;

public class StudentInfoSystem {
    private Scanner scanner = new Scanner(System.in);
    private StudentFileHandler fileHandler = new StudentFileHandler();
    private List<Student> students = fileHandler.loadStudents();

    public static void main(String[] args) {
        new StudentInfoSystem().run();
    }

    public void run() {
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    searchStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    System.out.println("退出程序。");
                    fileHandler.saveStudents(students);
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选项，请重新输入。");
                    break;
            }
        }
    }

    private void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (students.stream().anyMatch(s -> s.getId().equals(id))) {
            System.out.println("学号重复，请重新输入。");
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        students.add(student);
        fileHandler.saveStudents(students);
        System.out.println("学生信息添加成功！");
    }

    private void searchStudent() {
        while (true) {
            System.out.print("请输入要检索的学生学号（输入QUIT退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = students.stream()
                    .filter(s -> s.getId().equals(id))
                    .findFirst()
                    .orElse(null);
            if (student != null) {
                System.out.println("学生信息：" + student);
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void updateStudent() {
        while (true) {
            System.out.print("请输入要更新的学生学号（输入QUIT退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = students.stream()
                    .filter(s -> s.getId().equals(id))
                    .findFirst()
                    .orElse(null);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                fileHandler.saveStudents(students);
                System.out.println("学生信息更新成功！");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void deleteStudent() {
        while (true) {
            System.out.print("请输入要删除的学生学号（输入QUIT退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            boolean removed = students.removeIf(s -> s.getId().equals(id));
            if (removed) {
                fileHandler.saveStudents(students);
                System.out.println("学生信息删除成功！");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }
}